package com.joejag.mavenstats.client.views.sidebar;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.Widget;
import com.joejag.mavenstats.client.MavenStats;
import com.joejag.mavenstats.client.views.sidebar.children.Projects;
import com.joejag.mavenstats.client.views.sidebar.children.Reports;

public class SideBar extends Composite
{
  private Projects projectsView;

  public void refreshProjectsTree(MavenStats.Images images)
  {
    projectsView.refreshTree(images);
  }

  public interface Images extends Projects.Images, Reports.Images
  {
    /**
     * @gwt.resource com/joejag/mavenstats/client/images/contactsgroup.gif
     */
    AbstractImagePrototype projectsgroup();

    /**
     * @gwt.resource com/joejag/mavenstats/client/images/tasksgroup.gif
     */
    AbstractImagePrototype tasksgroup();

    /**
     * @gwt.resource com/joejag/mavenstats/client/images/ui/leftCorner.png
     */
    AbstractImagePrototype leftCorner();

    /**
     * @gwt.resource com/joejag/mavenstats/client/images/ui/rightCorner.png
     */
    AbstractImagePrototype rightCorner();
  }

  private int nextHeaderIndex = 0;

  private StackPanel stackPanel = new StackPanel()
  {
    public void onBrowserEvent(Event event)
    {
      int oldIndex = getSelectedIndex();
      super.onBrowserEvent(event);
      int newIndex = getSelectedIndex();
      if (oldIndex != newIndex)
      {
        updateSelectedStyles(oldIndex, newIndex);
      }
    }
  };

  public SideBar(Images images)
  {
    // Create the groups within the stack panel.
    projectsView = new Projects(images);
    add(images, projectsView, images.tasksgroup(), "Projects");
    add(images, new Reports(images), images.projectsgroup(), "Reports");

    initWidget(stackPanel);
  }

  protected void onLoad()
  {
    // Show the mailboxes group by default.
    stackPanel.showStack(0);
    updateSelectedStyles(-1, 0);
  }

  private void add(Images images, Widget widget, AbstractImagePrototype imageProto, String caption)
  {
    widget.addStyleName("mail-StackContent");
    stackPanel.add(widget, createHeaderHTML(images, imageProto, caption), true);
  }

  private String computeHeaderId(int index)
  {
    return "header-" + this.hashCode() + "-" + index;
  }

  private String createHeaderHTML(Images images, AbstractImagePrototype imageProto, String caption)
  {

    boolean isTop = (nextHeaderIndex == 0);
    String cssId = computeHeaderId(nextHeaderIndex);
    nextHeaderIndex++;

    String captionHTML = "<table class='caption' cellpadding='0' cellspacing='0'>"
        + "<tr><td class='lcaption'>" + imageProto.getHTML()
        + "</td><td class='rcaption'><b style='white-space:nowrap'>" + caption
        + "</b></td></tr></table>";

    return "<table id='" + cssId + "' align='left' cellpadding='0' cellspacing='0'"
        + (isTop ? " class='is-top'" : "") + "><tbody>"
        + "<tr><td class='box-00'>" + images.leftCorner().getHTML() + "</td>"
        + "<td class='box-10'>&nbsp;</td>"
        + "<td class='box-20'>" + images.rightCorner().getHTML() + "</td>"
        + "</tr><tr>"
        + "<td class='box-01'>&nbsp;</td>"
        + "<td class='box-11'>" + captionHTML + "</td>"
        + "<td class='box-21'>&nbsp;</td>"
        + "</tr></tbody></table>";
  }

  private void updateSelectedStyles(int oldIndex, int newIndex)
  {
    oldIndex++;
    if (oldIndex > 0 && oldIndex < stackPanel.getWidgetCount())
    {
      Element elem = DOM.getElementById(computeHeaderId(oldIndex));
      DOM.setElementProperty(elem, "className", "");
    }

    newIndex++;
    if (newIndex > 0 && newIndex < stackPanel.getWidgetCount())
    {
      Element elem = DOM.getElementById(computeHeaderId(newIndex));
      DOM.setElementProperty(elem, "className", "is-beneath-selected");
    }
  }
}
